﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace HYView
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class Partial3dView : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				CmdViewType types = (CmdViewType)9;
				if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					transaction = new Transaction(cmdData.Application.ActiveUIDocument.Document);
					transaction.Start("shows ectionBox in the 3dView");
					CreatePartial3dView createPartial3dView = new CreatePartial3dView(cmdData);
					View3D view3D = null;
					createPartial3dView.Create(ref view3D);
					transaction.Commit();
					if (view3D != null)
					{
						cmdData.Application.ActiveUIDocument.ActiveView = view3D;
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				msg = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
